package com.szgr.baoyin.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.szgr.baoyin.MyApplication;
import com.szgr.baoyin.R;
import com.szgr.baoyin.adapter.GoodsDetailOrderListAdapter;
import com.szgr.baoyin.adapter.MyOrderListAdapter;
import com.szgr.baoyin.model.GoodsDetailOrder;
import com.szgr.baoyin.myview.NoScrollListView;
import com.szgr.baoyin.utils.Constant;
import com.szgr.baoyin.utils.DateUtils;
import com.szgr.baoyin.utils.DialogUtils;
import com.szgr.baoyin.utils.RLog;
import com.szgr.baoyin.utils.ViewFindUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GoodsDetailActivity extends BaseActivity {
    private GoodsDetailOrder goodsDetailOrder;
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 403:
                    tv_sn.setText("订单编号：" + goodsDetailOrder.getOrder_sn());
                    tv_time.setText("下单时间：" + DateUtils.paseDate1(Long.parseLong(goodsDetailOrder.getAdd_time())));

                    GoodsDetailOrder.ExtendOrderCommonEntity extend_order_common = goodsDetailOrder.getExtend_order_common();
                    tv_name.setText(extend_order_common.getReciver_name());
                    GoodsDetailOrder.ExtendOrderCommonEntity.ReciverInfoEntity reciver_info = extend_order_common.getReciver_info();
                    tv_phone.setText(reciver_info.getPhone());
                    tv_address.setText(reciver_info.getAddress());

                    data = goodsDetailOrder.getExtend_order_goods();
                    tv_number.setText("共" + data.size() + "件商品");
                    tv_price.setText("¥ " + goodsDetailOrder.getOrder_amount() + "元");

                    listAdapter.setData(data);

                    DialogUtils.hideLoadingDialog();
                    break;
            }
        }
    };
    private TextView tv_sn;
    private TextView tv_time;
    private TextView tv_name;
    private TextView tv_phone;
    private TextView tv_address;
    private TextView tv_number;
    private TextView tv_price;
    private NoScrollListView listView;
    private List<GoodsDetailOrder.ExtendOrderGoodsEntity> data;
    private GoodsDetailOrderListAdapter listAdapter;
    private TextView tv_btn;
    private TextView tv_connect;
    private TextView tv_cancel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_goods_detail);
        super.onCreate(savedInstanceState);
    }

    @Override
    public void initActionBar() {
        actionBarTitle.setText("订单详情");
    }

    @Override
    public void initView() {
        tv_name = ViewFindUtils.find(view, R.id.my_order_detail_name);
        tv_phone = ViewFindUtils.find(view, R.id.my_order_detail_phone);
        tv_address = ViewFindUtils.find(view, R.id.my_order_detail_address);

        tv_sn = ViewFindUtils.find(view, R.id.add_sn);
        tv_time = ViewFindUtils.find(view, R.id.add_time);

        tv_number = ViewFindUtils.find(view, R.id.order_goods_num);
        tv_price = ViewFindUtils.find(view, R.id.order_goods_price);

        tv_btn = (TextView) view.findViewById(R.id.order_goods_btn);
        tv_connect = (TextView) view.findViewById(R.id.order_goods_btn2);
        tv_cancel = (TextView) view.findViewById(R.id.order_goods_btn1);

        listView = ViewFindUtils.find(view, R.id.order_no_scroll_list_view);
        data = new ArrayList<>();
        listAdapter = new GoodsDetailOrderListAdapter(this, data);
        listView.setAdapter(listAdapter);
        Intent intent = getIntent();
        if(intent != null && intent.hasExtra("id")){
            String id = intent.getStringExtra("id");
            getGoodsDetail(id);
        }
    }

    private void getGoodsDetail(final String id) {
        DialogUtils.showLoadingDialog(this,"正在加载中...");
        StringRequest sr = new StringRequest(Request.Method.POST, Constant.URL_GOOD_DETAIL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String s) {
                        RLog.s("---result--->" + s);
                        try {
                            JSONObject jsonObject = new JSONObject(s);
                            JSONObject datas = jsonObject.getJSONObject("datas");
                            JSONObject order_info = datas.getJSONObject("order_info");
                            goodsDetailOrder = new Gson().fromJson(order_info.toString(),GoodsDetailOrder.class);
                            mHandler.obtainMessage(403).sendToTarget();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> map = new HashMap<>();
                map.put("key",MyApplication.getmInstance().getKey());
                map.put("order_id",id);
                return map;
            }
        };
        MyApplication.getmInstance().addToRequestQueue(sr);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.action_bar_left:
                finish();
                break;
        }
    }
}
